<template>
	<view class="container">
		<!-- <image @click.stop="$navTo('/pages/user/vipShop')" src="https://img.jinghushi.com/images/9/2024/07/ED3HyuquhBHU3QIuQ9V1dI99Vv13vP.png" mode="widthFix" class="i_bg"></image> -->
		<!-- <image src="https://img.jinghushi.com/images/9/2024/08/n4161phB5QDDxPdReiSj65J5hzh4R4.png" mode="widthFix" class="i_bg"></image> -->
		<image src="https://img.jinghushi.com/images/0/2025/03/Wx99UX9MuuYeUul92XLe6FQtulU1ut.jpg" mode="widthFix" class="i_bg"></image>
		<!-- <view class="mp-search-box" style="background-image: url(https://img.jinghushi.com/images/9/2024/08/n4161phB5QDDxPdReiSj65J5hzh4R4.png);"> -->
		<view class="mp-search-box" style="background-image: url(https://img.jinghushi.com/images/0/2025/03/Wx99UX9MuuYeUul92XLe6FQtulU1ut.jpg);">
			<!-- <uni-icons type="back" size="24" color="#666" @click.stop="$navBack()"></uni-icons> -->
			<view class="locate" @click.stop="$navTo('/pages/shoplist/regionlist?city='+oldCity)">
				<text class="clamp">{{address}}</text>
				<text class="yticon icon-you"></text>
			</view>
			<view class="search" @click.stop="$navTo('/pages/product/searchstat')">
				<icon type="search" size="16" />
				<text>搜索门店</text>
			</view>
			<!-- <text class="yticon icon-saomiao" @click="scancode"></text> -->
		</view>
		<!-- <view v-if="!isDesktopCut" class="desktop_tip">
			<view class="up">▲</view>
			<image src="/static/close.png" @click="closeTip" mode="widthFix"></image>
			<text class="title">添加到手机桌面，使用多米街更便利</text>
			<view class="add" @click="">添加</view>
		</view> -->
		<view class="body">
			<jhome1 v-if="homes" ref="Jhomes"></jhome1>
		</view>
		<!-- <image class="home_kefu" @click="kefu" src="/static/Home_Icon/kefu.png" ></image> -->
		<!-- <view class="no_login" v-if="!isLogin">
			<view>终于等到你! 快来享受低价消费吧~</view>
			<view class="no_btn" @click="$navTo('/pages/public/login?mid='+mid)">马上登录</view>
		</view>
		<view class="no_login" v-if="!isInfo">
			<view>完善信息，可享受更多折扣哦！</view>
			<view class="no_btn" @click="$navTo('/pages/individualism/user')">去完善</view>
		</view> -->
	</view>
</template>
<script>
	import Jhome1 from '@/components-home/pages/Jhome1.vue'
	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			...mapState(['Location', ''])
		},
		components: {
			Jhome1
		},
		data() {
			return {
				homes: true,
				oldCity: '',
				city: '',
				address: '',
				isDesktopCut: false,
				isLogin: true,
				mid: 0,
				isInfo: true,
			};
		},
		async onLoad(options) {
			if (options.mid) {
				this.mid = options.mid;
			}
			if (options.scene) {
				this.mid = options.scene;
			}
			uni.setStorageSync('mid', this.mid);
			this.getLocation();
			await this.$onLaunched;
			//判断是否授权位置
			// uni.authorize({
			//     scope: 'scope.userLocation',
			//     success: () => {
			//         console.log('已授权');
			// 		this.getCity();
			//     },
			// 	fail: ()=> {
			// 		this.manualAuthorization()
			// 	},
			// })
			if(!uni.getStorageSync('token')){
				this.getToken();
			}
			this.getCity();
			uni.$on('cityChange', res =>
			{
				this.city = res.city;
				this.address = res.city;
				this.$store.commit('upcity', this.city);
				this.$store.commit('upaddress', this.city);
				this.$refs.Jhomes.$refs.skill.page = 0;
				this.$refs.Jhomes.$refs.skill.list = [];
				this.$refs.Jhomes.$refs.skill.getList();
			})
			this.isDesktopCut = uni.getStorageSync('isDesktopCut');
			setTimeout(()=>{
				this.closeTip();
			},4000)
		},
		onShow() {
			if (!uni.getStorageSync('token')) {
				this.isLogin = false;
			}else{
				this.isLogin = true;
				this.getUserInfo();
			}
		},
		onTabItemTap(e) {
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 3
			})
		},
		onReachBottom() {
			uni.$emit('GetList');
		},
		onPageScroll(e) {
			uni.$emit('Scroll');
		},
		methods: {
			getLocation() {
				let that = this;
				uni.getLocation({
					type: 'wgs84',
					altitude: true,
					success:res=> {
						console.log('成功',res)
						that.$store.commit('upLocation', {
							x: res.longitude,
							y: res.latitude
						})
						uni.setStorageSync('x',res.longitude);
						uni.setStorageSync('y',res.latitude);
						that.$isResolve();
					},
					fail:err=> {
						console.log('失败',err)
						that.$isResolve();
					},
					complete:com=> {
						console.log('失败',com)
						that.$isResolve();
					}
				});
			},
			
			manualAuthorization(){
				let that = this;
				uni.showModal({
					title: '提示', //提示的标题,
					content: '需要打开位置权限，以查看附近商家', //提示的内容,
					showCancel: false, //是否显示取消按钮,
					cancelText: '取消', //取消按钮的文字，默认为取消，最多 4 个字符,
					cancelColor: '#000000', //取消按钮的文字颜色,
					confirmText: '去授权', //确定按钮的文字，默认为取消，最多 4 个字符,
					confirmColor: '#4E07A9', //确定按钮的文字颜色,
					  success: res => {
						if (res.confirm) {
						  // openSetting 是需要事件驱动的，保证它的同步性。
						  uni.openSetting({
							success: res=> {
								console.log('用户授权选择完毕')
								/* #ifdef MP-WEIXIN */
								uni.startLocationUpdateBackground({
									type: 'gcj02',
									success() {
										uni.onLocationChange(function(res) {
											console.log('成功',res)
											that.$store.commit('upLocation', {
												x: res.longitude,
												y: res.latitude
											})
											uni.setStorageSync('x',res.longitude);
											uni.setStorageSync('y',res.latitude);
											that.getCity();
											uni.hideLoading();
										});
									},
									fail(err) {
										uni.startLocationUpdate({
											success: res => {
												uni.onLocationChange(function(res) {
													console.log('失败',res)
													that.$store.commit('upLocation', {
														x: res.longitude,
														y: res.latitude
													})
													uni.setStorageSync('x',res.longitude);
													uni.setStorageSync('y',res.latitude);
													that.getCity();
													uni.hideLoading();
												});
											},
											fail: err => {
												that.$api.msg('您未授权，无法获取精准商家信息')
												that.getCity();
											}
										});
									}
								});
								/* #endif */
								/* #ifdef MP-ALIPAY */
								uni.getLocation({
									type: '0',
									success:res=> {
										console.log('成功',res)
										that.$store.commit('upLocation', {
											x: res.longitude,
											y: res.latitude
										})
										uni.setStorageSync('x',res.longitude);
										uni.setStorageSync('y',res.latitude);
										that.getCity();
										uni.hideLoading();
									}
								});
								/* #endif */
							}
						  })
						} else if (res.cancel) {
							that.$api.msg('需要位置权限')
						}
					}
				});
			},
			getToken(){
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: loginRes=> {
						this.$axios('login/autologin','POST','member',{
							mid: this.mid,
							code: loginRes.code
						}).then(res=>{
							if(res.data.code == 200){
								uni.setStorageSync('token', res.data.data);
								uni.setStorageSync('Jh_recotabshops', 1);
								this.isLogin = true;
								this.getUserInfo();
								this.getSharePath();
							}
						})
					}
				})
				// #endif
				// #ifdef MP-ALIPAY
				my.getAuthCode({
				    scopes: 'auth_base',
				    success: res => {
						this.$axios('login/autologinAliPay','POST','member',{
							mid: this.mid,
							code: res.authCode
						}).then(res=>{
							if(res.data.code == 200){
								uni.setStorageSync('token', res.data.data);
								uni.setStorageSync('Jh_recotabshops', 1);
								this.isLogin = true;
								this.getUserInfo();
								this.getSharePath();
							}
						})
					}
				})
				// #endif
			},
			getUserInfo(){
				this.$axios('Center/main', "POST", 'member').then(res => {
					if (res.data.code == 200) {
						this.$store.commit('uplevel', res.data.data.levelname);
						uni.setStorageSync('level',res.data.data.levelname);
						if (res.data.data.isopenavatar){ 
							this.isInfo = false; 
						}else{
							this.isInfo = true;
						}
					}
				})
			},
			closeTip(){
				uni.setStorageSync('isDesktopCut', true);
				this.isDesktopCut = true;
			},
			getCity() {
					// this.$axios('o2o/getCurCity', 'POST', 'shop', this.Location).then(res => {
					this.$axios('o2o/curAddress', 'POST', 'shop', this.Location).then(res => {
						if (res.data.code == 200) {
							this.city = res.data.data.city;
							this.oldCity = res.data.data.city;
							this.address = res.data.data.address;
							this.$store.commit('upcity', this.city);
							this.$store.commit('upaddress', res.data.data.address);
							this.$refs.Jhomes.$refs.skill.getCateList();
						}
					})
			},
			kefu() {
				let that = this;
				wx.openCustomerServiceChat({
					extInfo: {
						url: 'https://work.weixin.qq.com/kfid/kfc85558bf452926a1b',
					},
					corpId: 'ww818270697fafba52',
					fail(res) {
						that.$api.msg('打开失败请重试！');
					}
				})
			},
			scancode() {
				let that = this;
				uni.scanCode({
					success: function(res) {
						let url = res.result;
						if (res.result.indexOf('https://h5.jinghushi.com/orderpay') != -1) {
							url = url.replace('https://h5.jinghushi.com/orderpay', '/pages/business/accounts');
						}
						that.$navTo(url);
					},
					fail(error) {
						that.$api.msg('扫码失败')
					}
				})
			}
		},
	}
</script>
<style lang="scss" scoped>
	.container{
		background: #FBF8FE;
	}
	.bg{
		background: $bg-color;
	}
	.h1_jcvideo_msg {
		position: fixed;
		top: 290px;
		right: 0;
		margin-right: 10px;
		opacity: 0;
		transition: opacity 1s linear;
	}

	.h1_jcvideo_msg_SH {
		opacity: 1
	}
	
	@font-face {
		font-family: iconfont;
		font-weight: normal;
		font-style: normal;
		src: url('../../static/iconfont.ttf') format('truetype');
	}

	.iconfont {
		font-family: "iconfont" !important;
		font-size: 16px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
	}

	.icon-kefu2:before {
		content: "\e769";
	}

	.icon-kefu {
		font-size: 30upx;
		color: #303133;
	}

	.wp-hkefu {
		position: fixed;
		top: 20upx;
		left: 8upx;
		background-color: #fff;
		width: 44upx;
		z-index: 1000;
	}
	.i_bg{
		width: 100%;
	}
	.mp-search-box {
		// background: $bg-color;
		color: #fff;
		position: fixed;
		left: 0;
		top: 0;
		padding: calc(var(--status-bar-height) + 50upx) 20upx 10upx;
		display: flex;
		align-items: center;
		width: 100%;
		z-index: 999;
		background-size: 100%;
		color: #333333;
		.locate {
			font-size: 30upx;
			display: flex;
			max-width: calc(100% - 350rpx);
			// #ifdef MP-ALIPAY
			margin-left: 50rpx;
			// #endif
			.icon-you {
				display: inline-block;
				transform: rotateZ(90deg);
			}
		}

		.search {
			background: #FFFFFF;
			border-radius: 40px;
			color: #909399;
			display: flex;
			align-items: center;
			font-size: 28upx;
			height: 55upx;
			margin: 0 10upx;
			padding: 0 10upx;
			width: 320upx;

			text {
				margin-left: 10upx;
			}
		}

		.icon-saomiao {
			font-size: 50upx;
		}
	}
.desktop_tip{
		background: rgba(255, 255, 255, 0.9);
		color: #333;
		font-size: 22rpx;
		border-radius: 10rpx;
		position: fixed;
		width: 78%;
		left: 0;
		right: 0;
		margin: auto;
		height: 60rpx;
		line-height: 60rpx;
		z-index: 99999999;
		top: calc(var(--status-bar-height) + 125rpx);
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.up{
			position: absolute;
			color: #fff;
			right: 8%;
			height: 20rpx;
			top: -15rpx;
			line-height: 1;
		}
		image{
			width: 18rpx;
			height: 18rpx;
		}
		.title{
			flex: 1;
			margin-left: 20rpx;
			color: #000;
		}
		.add{
			width: 80rpx;
			height: 40rpx;
			line-height: 40rpx;
			border-radius: 40rpx;
			background: $bg-color;
			color: #fff;
			text-align: center;
		}
	}
	.body {
		position: relative;
		// padding-top: calc(var(--status-bar-height) + 90upx);
		z-index: 100;
		// margin-top: -250rpx;
		// margin-top: -540rpx;
		padding: 20rpx;
		margin-top: -80rpx;
	}

	.home_kefu {
		position: fixed;
		border-radius: 50%;
		bottom: 180upx;
		height: 90upx;
		width: 90upx;
		right: 40upx;
		z-index: 100;
	}

	.home_kefu image {
		height: 90upx;
		width: 90upx;
	}
</style>